package com.soft.app.ruanba;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.lang.StringUtils;
import org.xml.sax.SAXException;

import com.soft.app.ruanba.down.FileDownloadHandler;
import com.soft.app.ruanba.down.FileDownloadHelper;
import com.soft.app.ruanba.down.FileVo;
import com.soft.app.ruanba.down.ImgDownloadHandler;
import com.soft.app.ruanba.down.ImgVo;

public class ImgDownloadJob {
	private String imgDownloadDir;
	private String xmlFile;
	
	public ImgDownloadJob(String imgDownloadDir, String xmlFile){
		this.imgDownloadDir = imgDownloadDir;
		this.xmlFile = xmlFile;
	}
	
	public void excute(){
		//xml 解析
		try {   
			SAXParserFactory sax = SAXParserFactory.newInstance();
			SAXParser parser = sax.newSAXParser();
			InputStream is = new FileInputStream(xmlFile);
			ImgDownloadHandler idh = new ImgDownloadHandler();
			parser.parse(is, idh);
			List<ImgVo> images = idh.getImages();
			for(ImgVo vo: images){
				if(StringUtils.isNotBlank(vo.getName())){
					System.out.println(vo.getName());
					System.out.println(vo.getFileName());
					System.out.println(vo.getThumbName());
					System.out.println(vo.getThumbFileName());
					FileDownloadHelper.download(imgDownloadDir, vo.getFileName(), "jpg", vo.getName());
					FileDownloadHelper.download(imgDownloadDir, vo.getThumbFileName(), "jpg", vo.getThumbName());
				}
			}
		} catch (ParserConfigurationException e) {   
			e.printStackTrace();   
		} catch (SAXException e) {   
			e.printStackTrace();   
		} catch (FileNotFoundException e) {   
			e.printStackTrace();   
		} catch (IOException e) {   
			e.printStackTrace();   
		}   
	}
	
	public String getImgDownloadDir() {
		return imgDownloadDir;
	}
	public void setImgDownloadDir(String imgDownloadDir) {
		this.imgDownloadDir = imgDownloadDir;
	}
	public String getXmlFile() {
		return xmlFile;
	}
	public void setXmlFile(String xmlFile) {
		this.xmlFile = xmlFile;
	}
	
}
